﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic;
using DataAccess;
using System.Data;

namespace CarService
{
    public partial class News : System.Web.UI.Page
    {
        NewsBusiness newsBusiness = new NewsBusiness();

        private int NewsID
        {
            get
            {
                try
                {
                    if (Request.QueryString["ID"] != null)
                    {
                        return Convert.ToInt32(Request.QueryString["ID"]);
                    } 
                    else
                    {
                        return newsBusiness.SelectNewNewsID(1);
                    }
                }
                catch (System.Exception)
                {
                    return -1;
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            Load_Info();
            if (!Page.IsPostBack)
            {
                Load_Data();
                Load_News();
            }
        }

        private void Load_Info()
        {
            SettingInfo settingInfo = SettingData.SelectBySiteID(Config.Global.Settings.SiteID);
            Page.MetaDescription = settingInfo.SiteDescription;
            Page.MetaKeywords = settingInfo.SiteKeyword;
        }

        private void Load_Data()
        {
            NewsInfo newsInfo = NewsData.SelectByNewsID(NewsID);
            Page.Title = newsInfo.NewsTitle;

            lblTitle.Text = newsInfo.NewsTitle;
            lblDatePosted.Text = newsInfo.DatePosted.ToString("dd/MM/yyyy hh:mm:ss");
            lblDesc.Text=newsInfo.NewsDescription; 
            lblContent.Text = newsInfo.NewsContent;
        }

        private void Load_News()
        {
            DataTable dtbNews = newsBusiness.SelectOtherNews(NewsID, 3);
            if ((dtbNews != null) && (dtbNews.Rows.Count > 0))
            {
                rptNews.DataSource = dtbNews;
                rptNews.DataBind();
            }
        }
    }
}